﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Snowing.Common
{
    public class RandomHelper
    {
        public static ulong GenerateRandomLong(int length)
        {
            ulong result = RandomLong(length);
            while (result.ToString().Contains("666") || result.ToString().Contains("888") || result < Math.Pow(10, length - 1))
            {
                result = RandomLong(length);
            }
            return result;
        }

        public static string GenerateMtRandomID(int length, string prefix = "mt")
        {
            string src = "0123456789";
            string target = Random(src, length);
            return prefix + target;
        }

        /// <summary>
        /// 0-9, a-z, A-Z 随机字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateRandomString(int length)
        {
            string src = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return Random(src, length);
        }

        protected static string Random(string src, int length)
        {
            string target = string.Empty;
            for (int i = 0; i < length; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                int randomInt = random.Next(0, src.Length - 1);
                target += src[randomInt];
            }
            return target;
        }

        private static ulong RandomLong(int length)
        {
            ulong result = 0;

            for (int i = 0; i < length; i++)
            {
                Random random = new Random(Guid.NewGuid().GetHashCode());
                int randomInt = random.Next(0, 9);
                result += (ulong)(randomInt * Math.Pow(10, i));
            }

            return result;

        }
    }
}
